package com.project.junM.service.impl;

import com.alibaba.fastjson.support.odps.CodecCheck;
import com.project.junM.entity.*;
import com.project.junM.mapper.*;
import com.project.junM.service.TSetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TSetmealServiceImpl implements TSetmealService {

    @Autowired
    private TSetmealMapper tSetmealMapper;
    @Autowired
    private TSetmealCheckgroupMapper tSetmealCheckgroupMapper;
    @Autowired
    private TCheckgroupMapper tCheckgroupMapper;
    @Autowired
    private TCheckgroupCheckitemMapper tCheckgroupCheckitemMapper;
    @Autowired
    private TCheckitemMapper tCheckitemMapper;

    @Override
    public List<TSetmeal> querySetMealAll() {

        return tSetmealMapper.querySetMealAll();
    }

    @Override
    public List<TSetmeal> queryBySexAll(String sex) {
        return tSetmealMapper.queryBySexAll(sex);
    }

    @Override
    public List<TSetmeal> selectByPrimaryKey(Integer id) {
//        //套餐Id
 //       TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(id);
//        //获取套餐Id对应的项目名称
//        List<TSetmealCheckgroup> tSetmealCheckgroups = tSetmealCheckgroupMapper.selectBySetmealId(id);
////        tSetmeal.setTSetmealCheckgroups(tSetmealCheckgroups);
//        //获取项目Id查询出对应项目
//        ArrayList<TCheckgroup> tCheckgroups = new ArrayList<>();
//        for (int i=0;i<tSetmealCheckgroups.size();i++){
//            Integer checkgroupId = tSetmealCheckgroups.get(i).getCheckgroupId();
//            System.out.println(checkgroupId);
//            TCheckgroup tCheckgroup = tCheckgroupMapper.selectByPrimaryKey(checkgroupId);
//            tCheckgroups.add(tCheckgroup);
//        }
//        tSetmeal.setTCheckgroups(tCheckgroups);
//        //获取项目详细信息
//        ArrayList<List<TCheckgroupCheckitem>> tCheckgroupCheckitems = new ArrayList<>();
//        for(int j=0;j<tCheckgroups.size();j++){
//            Integer checkgroupId = tCheckgroups.get(j).getId();
//            System.out.println(checkgroupId);
//            List<TCheckgroupCheckitem> checkitems = tCheckgroupCheckitemMapper.selectByCheckgrounpId(checkgroupId);
//            tCheckgroupCheckitems.add(checkitems);
//        }
//        ArrayList<TCheckitem> tCheckitems = new ArrayList<>();
//        List<TSetmeal> setmeals = new ArrayList<>();

        List<TSetmeal> tSetmeals = new ArrayList<>();
        TSetmeal tSetmeal = tSetmealMapper.selectByPrimaryKey(id);
        List<TCheckgroup> tCheckgroups=new ArrayList<>();
        List<TSetmealCheckgroup> tSetmealCheckgroups = tSetmealCheckgroupMapper.selectBySetmealId(id);
        for (TSetmealCheckgroup tSetmealCheckgroup : tSetmealCheckgroups) {
            TCheckgroup t=tCheckgroupMapper.selectByPrimaryKey(tSetmealCheckgroup.getCheckgroupId());
            List<TCheckgroupCheckitem> tCheckgroupCheckitems = tCheckgroupCheckitemMapper.selectByCheckgrounpId(tSetmealCheckgroup.getCheckgroupId());
                List<TCheckitem> tCheckitems=new ArrayList<>();
                for (TCheckgroupCheckitem tCheckgroupCheckitem : tCheckgroupCheckitems) {
                    tCheckitems.add(tCheckitemMapper.selectByPrimaryKey(tCheckgroupCheckitem.getCheckitemId()));
                    t.setTCheckitems(tCheckitems);
                }
            tCheckgroups.add(t);
        }
        tSetmeal.setTCheckgroups(tCheckgroups);
        tSetmeals.add(tSetmeal);

       /* for (int k=0;k<tCheckgroupCheckitems.size();k++){


            List<TCheckgroupCheckitem> checkgroupIds = (List<TCheckgroupCheckitem>) tCheckgroupCheckitems.get(k);
            for(int i=0 ;i<checkgroupIds.size();i++){
                Integer checkitemId = checkgroupIds.get(i).getCheckitemId();
                System.out.println(checkitemId);
                TCheckitem tCheckitem = tCheckitemMapper.selectByPrimaryKey(checkitemId);
                tCheckitems.add(tCheckitem);
            }
            tSetmeal.setTCheckitems(tCheckitems);
            for(int i=0;i<tSetmeal.getTCheckgroups().size();i++){
                TCheckgroup tCheckgroup = tSetmeal.getTCheckgroups().get(i);
                System.out.println(tCheckgroup);
                System.out.println(tCheckgroup.getId());
                String name = tCheckgroup.getName();
                tSetmeal.setProjectName(name);
                String names="";
                double sumPrice=0;
                for (int j = 0; j < tSetmeal.getTCheckitems().size(); j++) {
                    TCheckitem tCheckitem = tSetmeal.getTCheckitems().get(j);
                    System.out.println(tCheckitem);
                    String name1 = tCheckitem.getName();
                    names += name1 + "、";
                    sumPrice += tCheckitem.getPrice();
                }
                tSetmeal.setProjectContent(names.substring(0, names.length() - 1));
                tSetmeal.setProjectSumPrice(sumPrice + "元");
                setmeals.add(tSetmeal);
                System.out.println(tSetmeal);
            }
            tSetmeal.setTCheckitems(null);
        }*/
        return tSetmeals;
    }

}
